home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MotifGraphicsUtils.java < prev    next >
Text File  |  1998-06-30  |  15KB  |  470 lines

  1. /*
  2.  * @(#)MotifGraphicsUtils.java    1.23 98/02/04
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.plaf.motif;
  21.  
  22. import com.sun.java.swing.*;
  23. import java.awt.Color;
  24. import java.awt.Dimension;
  25. import java.awt.Graphics;
  26. import java.awt.Font;
  27. import java.awt.FontMetrics;
  28. import java.awt.Rectangle;
  29. import java.awt.Component;
  30. import java.awt.Insets;
  31. import java.awt.event.KeyEvent;
  32.  
  33. import com.sun.java.swing.plaf.basic.*;
  34. /*
  35.  * @version 1.23 02/04/98
  36.  * @author Jeff Dinkins
  37.  * @author Dave Kloba
  38.  */
  39.  
  40. public class MotifGraphicsUtils implements SwingConstants 
  41. {
  42.     public static final Font controlFont = new Font("Dialog", Font.PLAIN, 12);
  43.  
  44.     public static Color control = new Color(192,192,192);
  45.     public static Color controlLight = new Color(231, 231, 231);
  46.     public static Color controlShadow = new Color(102,102,102);
  47.     public static Color controlLightShadow = new Color(155,155,155);
  48.     public static Color controlHighlight = Color.white;
  49.     public static Color controlDisabled = new Color(117, 117, 117);
  50.     public static Color controlBlack = Color.black;
  51.     public static Color controlWhite = Color.white;
  52.     
  53.  
  54.     // ScrollBarButton colors
  55.     public final static Color gray231 = new Color(231, 231, 231);
  56.     public final static Color gray153 = new Color(153, 153, 153);
  57.     public final static Color gray102 = new Color(102, 102, 102);
  58.  
  59.     /**
  60.      * Convenience method for drawing a button bezel.
  61.      */
  62.     static void drawButtonBezel(Graphics g, int x, int y,
  63.                 int width, int height, boolean raised)
  64.     {
  65.     // ** Paint the button in
  66.     if(raised) {
  67.         g.setColor(MotifGraphicsUtils.controlWhite);
  68.         g.drawLine(x+1, y+1, x+1, y+1);
  69.         
  70.         g.setColor(MotifGraphicsUtils.controlLight);
  71.         g.drawLine(x, y, x+width-3, y);
  72.         g.drawLine(x, y, x, y+height-4);
  73.         
  74.         g.setColor(controlLightShadow);
  75.         g.drawLine(x+1, y+height-2, x+width-3, y+height-2);
  76.         g.drawLine(x+width-2, y+height-1, x+width-2, y+1);
  77.         
  78.         g.drawLine(x+width-3, y+height-3, x+width-3, y+height-3);
  79.         g.drawLine(x+width-1, y+0, x+width-1, y+0);
  80.         g.drawLine(x+0, y+height-1, x+0, y+height-1);
  81.         
  82.         g.setColor(MotifGraphicsUtils.controlShadow);
  83.         g.drawLine(x+1, y+height-1, x+width-1, y+height-1);
  84.         g.drawLine(x+width-1, y+height-2, x+width-1, y+1);
  85.         g.drawLine(x+width-2, y+height-2, x+width-2, y+height-2);
  86.     } else {
  87.         g.setColor(controlWhite);
  88.         g.drawLine(x + width-2, y + height-2, x + width-2, y + height-2);
  89.         
  90.         g.setColor(controlLight);
  91.         g.drawLine(x+2,       y+height-1,  x+width-1, y+height-1);
  92.         g.drawLine(x+width-1, y+height-2,  x+width-1, y+3);
  93.         
  94.         g.setColor(controlLightShadow);
  95.         g.drawLine(x+2, y+1, x+width-2, y+1);
  96.         g.drawLine(x+1, y+2, x+1,       y+height-2);
  97.         
  98.         g.drawLine(width-1, y,          x+width-1, y);
  99.         g.drawLine(x+2,     y+2,        x+2,       y+2);
  100.         g.drawLine(x,       y+height-1, x+0,       y+height-1);
  101.         
  102.         g.setColor(controlShadow);
  103.         g.drawLine(x,   y,   x+width-2, y);
  104.         g.drawLine(x,   y+1, x,         y+height-2);
  105.         g.drawLine(x+1, y+1, x+1,       y+1);
  106.     }
  107.     }
  108.     
  109.     /**
  110.      * Convenience method for drawing a raised ScrollBar Button bezel.
  111.      */
  112.     static void drawRaisedScrollButtonBezel(Graphics g, int x, int y,
  113.                                             int width, int height) {
  114.         int maxX, maxY;
  115.     
  116.         maxX = x + width;
  117.         maxY = y + height;
  118.  
  119.         g.setColor(Color.white);
  120.         g.drawLine(x, y + 1, x, maxY - 3);
  121.         g.drawLine(x + 1, y, maxX - 3, y);
  122.  
  123.         g.setColor(gray231);
  124.         drawPoint(g, x, y);
  125.         drawPoint(g, x, maxY - 2);
  126.         drawPoint(g, maxX - 2, y);
  127.  
  128.         g.setColor(gray153);
  129.         drawPoint(g, maxX - 1, y);
  130.         drawPoint(g, maxX - 1, maxY - 1);
  131.         drawPoint(g, x, maxY - 1);
  132.  
  133.         g.setColor(gray102);
  134.         g.drawLine(maxX - 1, y + 1, maxX - 1, maxY - 2);
  135.         g.drawLine(x + 1, maxY - 1, maxX - 2, maxY - 1);
  136.     }
  137.  
  138.     /**
  139.      * Convenience method for drawing a lowered ScrollBar Button bezel.
  140.      */
  141.     static void drawLoweredScrollButtonBezel(Graphics g, int x, int y,
  142.                                              int width, int height) {
  143.         int maxX, maxY;
  144.  
  145.         maxX = x + width;
  146.         maxY = y + height;
  147.  
  148.         g.setColor(gray153);
  149.         g.drawLine(x, maxY - 1, maxX - 1, maxY - 1);
  150.         g.drawLine(maxX - 1, y, maxX - 1, maxY - 2);
  151.  
  152.         g.setColor(Color.lightGray);
  153.         g.drawLine(x, y, maxX - 2, y);
  154.         g.drawLine(x, y + 1, x, maxY - 2);
  155.     }
  156.     
  157.     /**
  158.      * Draws the point (<b>x</b>, <b>y</b>) in the current color.
  159.      */
  160.     static void drawPoint(Graphics g, int x, int y) {
  161.         g.drawLine(x, y, x, y);
  162.     }
  163.  
  164.     /**
  165.      * Draws the Image tiled in the supplied rectangle.  Calls
  166.      * <b>paint()</b> for each tile.
  167.      */
  168.     static void drawTiledIcon(Component c, Icon icon, Graphics g1, int x, int y, int width, int height) {
  169.         Rectangle clipRect;
  170.         int imageWidth, imageHeight, minX, minY, maxX, maxY;
  171.  
  172.         Graphics g = g1.create();
  173.  
  174.         clipRect = g1.getClipBounds();
  175.  
  176.         imageWidth = icon.getIconWidth();
  177.         imageHeight = icon.getIconHeight();
  178.  
  179.         if (imageWidth <= 0 || imageHeight <= 0)
  180.             return;
  181.  
  182.         g.setClip(x, y, width, height);
  183.  
  184.         if (x > clipRect.x)
  185.             minX = x;
  186.         else
  187.             minX = x + imageWidth * ((clipRect.x - x) / imageWidth);
  188.  
  189.         if ((x + width) < (clipRect.x + clipRect.width))
  190.             maxX = x + width;
  191.         else
  192.             maxX = (clipRect.x + clipRect.width);
  193.  
  194.         if (y > clipRect.y)
  195.             minY = y;
  196.         else
  197.             minY= y + imageHeight * ((clipRect.y - y) / imageHeight);
  198.  
  199.         if ((y + height) < (clipRect.y + clipRect.height))
  200.             maxY = (y + height);
  201.         else
  202.             maxY = (clipRect.y + clipRect.height);
  203.  
  204.         for (x = minX; x < maxX; x += imageWidth) {
  205.             for (y = minY; y < maxY; y += imageHeight) {
  206.                 icon.paintIcon(c, g, x, y);
  207.             }
  208.         }
  209.         g.dispose();
  210.     }
  211.  
  212.     /*
  213.      * Convenience method for drawing a grooved line
  214.      *
  215.      */
  216.     public static void drawGroove(Graphics g, int x, int y, int w, int h)
  217.     {
  218.     Color oldColor = g.getColor();    // Make no net change to g
  219.     g.translate(x, y);
  220.  
  221.     g.setColor(controlShadow);
  222.     g.drawRect(0, 0, w-2, h-2);
  223.  
  224.     g.setColor(controlWhite);
  225.     g.drawLine(1, h-3, 1, 1);
  226.     g.drawLine(1, 1, w-3, 1);
  227.  
  228.     g.drawLine(0, h-1, w-1, h-1);
  229.     g.drawLine(w-1, h-1, w-1, 0);
  230.  
  231.     g.translate(-x, -y);
  232.     g.setColor(oldColor);
  233.     }
  234.  
  235.     /** Draws <b>aString</b> in the rectangle defined by
  236.       * (<b>x</b>, <b>y</b>, <b>width</b>, <b>height</b>).
  237.       * <b>justification</b> specifies the text's justification, one of
  238.       * LEFT, CENTER, or RIGHT.
  239.       * <b>drawStringInRect()</b> does not clip to the rectangle, but instead
  240.       * uses this rectangle and the desired justification to compute the point
  241.       * at which to begin drawing the text.
  242.       * @see #drawString
  243.       */
  244.     public static void drawStringInRect(Graphics g, String aString, int x, int y,
  245.                                  int width, int height, int justification) {
  246.         FontMetrics  fontMetrics;
  247.         int          drawWidth, startX, startY, delta;
  248.  
  249.         if (g.getFont() == null) {
  250. //            throw new InconsistencyException("No font set");
  251.             return;
  252.         }
  253.         fontMetrics = g.getFontMetrics();
  254.         if (fontMetrics == null) {
  255. //            throw new InconsistencyException("No metrics for Font " + font());
  256.             return;
  257.         }
  258.  
  259.         if (justification == CENTER) {
  260.             drawWidth = fontMetrics.stringWidth(aString);
  261.             if (drawWidth > width) {
  262.                 drawWidth = width;
  263.             }
  264.             startX = x + (width - drawWidth) / 2;
  265.         } else if (justification == RIGHT) {
  266.             drawWidth = fontMetrics.stringWidth(aString);
  267.             if (drawWidth > width) {
  268.                 drawWidth = width;
  269.             }
  270.             startX = x + width - drawWidth;
  271.         } else {
  272.             startX = x;
  273.         }
  274.  
  275.         delta = (height - fontMetrics.getAscent() - fontMetrics.getDescent()) / 2;
  276.         if (delta < 0) {
  277.             delta = 0;
  278.         }
  279.  
  280.         startY = y + height - delta - fontMetrics.getDescent();
  281.  
  282.         g.drawString(aString, startX, startY);
  283.     }
  284.  
  285.  
  286.   public static void paintMenuItem(Graphics g, JComponent c,
  287.                    Icon checkIcon, Icon arrowIcon,
  288.                    Color background, Color foreground,
  289.                    int defaultTextIconGap)
  290.     {
  291.  
  292.     JMenuItem b = (JMenuItem) c;
  293.       ButtonModel model = b.getModel();
  294.  
  295.       Dimension size = b.getSize();
  296.       Insets i = c.getInsets();
  297.       
  298.       Rectangle viewRect = new Rectangle(size);
  299.  
  300.       viewRect.x += i.left;
  301.       viewRect.y += i.top;
  302.       viewRect.width -= (i.right + viewRect.x);
  303.       viewRect.height -= (i.bottom + viewRect.y);
  304.  
  305.       Rectangle iconRect = new Rectangle();
  306.       Rectangle textRect = new Rectangle();
  307.       Rectangle acceleratorRect = new Rectangle();
  308.       Rectangle checkRect = new Rectangle();
  309.       Rectangle arrowRect = new Rectangle();
  310.  
  311.       Font holdf = g.getFont();
  312.       Font f = c.getFont();
  313.       g.setFont(f);
  314.       FontMetrics fm = g.getFontMetrics(f);
  315.       FontMetrics fmAccel = g.getFontMetrics( UIManager.getFont("MenuItem.acceleratorFont") );
  316.  
  317.       if (c.isOpaque()) {
  318.       if (model.isArmed()|| (c instanceof JMenu && model.isSelected())) {
  319.           g.setColor(background);
  320.       } else {
  321.           g.setColor(c.getBackground());
  322.       }
  323.       g.fillRect(0,0, size.width, size.height);
  324.       }
  325.  
  326.       // get Accelerator text
  327.       KeyStroke accelerator =  b.getAccelerator();
  328.       String acceleratorText = "";
  329.       if (accelerator != null) {
  330.       int modifiers = accelerator.getModifiers();
  331.       if (modifiers > 0) {
  332.           acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
  333.           acceleratorText += "+";
  334.       }
  335.       acceleratorText += KeyEvent.getKeyText(accelerator.getKeyCode());
  336.       }
  337.  
  338.       // layout the text and icon
  339.       String text = BasicGraphicsUtils.layoutMenuItem(
  340.                           fm, b.getText(), fmAccel,
  341.                           acceleratorText, b.getIcon(),
  342.                           checkIcon, arrowIcon,
  343.                           b.getVerticalAlignment(), 
  344.                           b.getHorizontalAlignment(),
  345.                           b.getVerticalTextPosition(), 
  346.                           b.getHorizontalTextPosition(),
  347.                           viewRect, iconRect, 
  348.                           textRect, acceleratorRect,
  349.                           checkRect, arrowRect,
  350.                           b.getText() == null ? 0 : defaultTextIconGap,
  351.                           defaultTextIconGap
  352.                           );
  353.       
  354.       // Paint the Check
  355.       Color holdc = g.getColor();
  356.       if (checkIcon != null) {
  357.     if(model.isArmed() || (c instanceof JMenu && model.isSelected()))
  358.       g.setColor(foreground);
  359.     checkIcon.paintIcon(c, g, checkRect.x, checkRect.y);
  360.     g.setColor(holdc);
  361.       }
  362.  
  363.       // Paint the Icon
  364.       if(b.getIcon() != null) { 
  365.     Icon icon;
  366.     if(!model.isEnabled()) {
  367.       icon = (Icon) b.getDisabledIcon();
  368.     } else if(model.isPressed() && model.isArmed()) {
  369.       icon = (Icon) b.getPressedIcon();
  370.       if(icon == null) {
  371.         // Use default icon
  372.         icon = (Icon) b.getIcon();
  373.       } 
  374.     } else {
  375.       icon = (Icon) b.getIcon();
  376.     }
  377.           
  378.     icon.paintIcon(c, g, iconRect.x, iconRect.y);
  379.       }
  380.  
  381.       // Draw the Text
  382.       if(text != null && !text.equals("")) {
  383.     if(!model.isEnabled()) {
  384.       // *** paint the text disabled
  385.                 g.setColor(b.getBackground().brighter());
  386.                 BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  387.                                               textRect.x, textRect.y + fm.getAscent());
  388.                 g.setColor(b.getBackground().darker());
  389.                 BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  390.                                               textRect.x - 1, textRect.y + fm.getAscent() - 1);
  391.  
  392.         //      g.setColor( Color.white ); // light gray??
  393.         //BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  394.         //textRect.x, textRect.y + fm.getAscent());
  395.     } else {
  396.       // *** paint the text normally
  397.       if (model.isArmed()|| (c instanceof JMenu && model.isSelected())) {
  398.         g.setColor(foreground);
  399.       } else {
  400.         g.setColor(b.getForeground());
  401.       }
  402.       BasicGraphicsUtils.drawString(g,text, 
  403.                     model.getMnemonic(),
  404.                     textRect.x,
  405.                     textRect.y + fm.getAscent());
  406.     }
  407.       }
  408.       
  409.       // Draw the Accelerator Text
  410.       if(acceleratorText != null && !acceleratorText.equals("")) {
  411.       g.setFont( UIManager.getFont("MenuItem.acceleratorFont") );
  412.       if(!model.isEnabled()) {
  413.           // *** paint the acceleratorText disabled
  414.           g.setColor(b.getBackground().brighter());
  415.           BasicGraphicsUtils.drawString(g,acceleratorText,model.getMnemonic(),
  416.                         acceleratorRect.x, acceleratorRect.y + fm.getAscent());
  417.           g.setColor(b.getBackground().darker());
  418.           BasicGraphicsUtils.drawString(g,acceleratorText,model.getMnemonic(),
  419.                         acceleratorRect.x - 1, acceleratorRect.y + fm.getAscent() - 1);
  420.       } else {
  421.           // *** paint the acceleratorText normally
  422.           if (model.isArmed()|| (c instanceof JMenu && model.isSelected()))
  423.           {
  424.               g.setColor(foreground);
  425.           } else {
  426.               g.setColor(b.getForeground());
  427.           }
  428.           BasicGraphicsUtils.drawString(g,acceleratorText, 
  429.                         model.getMnemonic(),
  430.                         acceleratorRect.x,
  431.                         acceleratorRect.y + fm.getAscent());
  432.       }
  433.       }
  434.  
  435.       // Paint the Arrow
  436.       if (arrowIcon != null) {
  437.     if(model.isArmed() || (c instanceof JMenu && model.isSelected()))
  438.       g.setColor(foreground);
  439.     if( !(b.getParent() instanceof JMenuBar) )
  440.       arrowIcon.paintIcon(c, g, arrowRect.x, arrowRect.y);
  441.       }
  442.  
  443.       g.setColor(holdc);
  444.       g.setFont(holdf);
  445.     }
  446.  
  447.   private static void drawMenuBezel(Graphics g, Color background,
  448.                     int x, int y,
  449.                     int width, int height)
  450.     {
  451.       // shadowed button region
  452.       g.setColor(background);
  453.       g.fillRect(x,y,width,height);
  454.  
  455.       g.setColor(background.brighter().brighter());
  456.       g.drawLine(x+1,       y+height-1,  x+width-1, y+height-1);
  457.       g.drawLine(x+width-1, y+height-2,  x+width-1, y+1);
  458.         
  459.       g.setColor(background.darker().darker());
  460.       g.drawLine(x,   y,   x+width-2, y);
  461.       g.drawLine(x,   y+1, x,         y+height-2);
  462.       
  463.     }
  464.  
  465.  
  466. }
  467.  
  468.  
  469.  
  470.